import random

def generate_lottery_numbers():
    """生成8个1-33的不重复随机数作为中奖号码"""
    return random.sample(range(1, 34), 8)

#手动生成用户号码
# def get_user_numbers():
#     """获取用户输入的8个数字"""
#     while True:
#         try:
#             user_input = input("请输入8个1-33的数字（用空格分隔）: ")
#             numbers = list(map(int, user_input.split()))
#             if len(numbers) != 8:
#                 print("请输入8个数字！")
#                 continue
#             if any(num < 1 or num > 33 for num in numbers):
#                 print("数字必须在1到33之间！")
#                 continue
#             if len(set(numbers)) != 8:
#                 print("数字不能重复！")
#                 continue
#             return numbers
#         except ValueError:
#             print("请输入有效的整数！")

#模拟用户输入的号码
def get_user_numbers():
    """模拟用户随机选择8个1-33的数字"""
    return random.sample(range(1, 34), 8)

def compare_numbers(lottery_numbers, user_numbers):
    matched_count = 0  # 初始化匹配计数器

    # 遍历用户输入的每个号码
    for num in user_numbers:
        # 如果号码在中奖号码列表中，则增加匹配计数器
        if num in lottery_numbers:
            matched_count += 1

    return matched_count

# def main1():
#     # 生成中奖号码
#     lottery_numbers = generate_lottery_numbers()
#
#
#     # 获取用户输入
#     user_numbers = get_user_numbers()
#
#     # 比较号码
#     matched_count = compare_numbers(lottery_numbers, user_numbers)
#
#     # 判断奖项
#     if matched_count == 8:
#         print("恭喜你，中了一等奖！")
#     elif matched_count == 7:
#         print("恭喜你，中了二等奖！")
#     elif matched_count == 6:
#         print("恭喜你，中了三等奖！")
#     elif matched_count == 5:
#         print("恭喜你，中了四等奖！")
#     else:
#         print("很遗憾，没有中奖。")
#     print("中奖号码:", lottery_numbers)
# if __name__ == "__main__":
#     main1()

def simulate_lottery(times,lottery_numbers):
    """
    模拟购买彩票多次，并统计各奖项的概率

    参数:
        times (int): 模拟次数

    返回:
        dict: 各奖项的统计结果
    """
    # 初始化奖项计数器
    prize_counts = {
        "一等奖": 0,
        "二等奖": 0,
        "三等奖": 0,
        "四等奖": 0,
        "未中奖": 0
    }

    # 存储各等奖的中奖号码
    prize_details = {
        "一等奖": [],
        "二等奖": [],
        "三等奖": [],
        "四等奖": [],
        #"未中奖": []
    }

    for i in range(times):
        #用户号码
        user_numbers = get_user_numbers()

        # 比较号码并统计奖项
        matched_count = compare_numbers(lottery_numbers, user_numbers)
        if matched_count == 8:
            prize_counts["一等奖"] += 1
            prize_details["一等奖"].append((lottery_numbers, user_numbers))
        elif matched_count == 7:
            prize_counts["二等奖"] += 1
            prize_details["二等奖"].append((lottery_numbers, user_numbers))
        elif matched_count == 6:
            prize_counts["三等奖"] += 1
            prize_details["三等奖"].append((lottery_numbers, user_numbers))
        elif matched_count == 5:
            prize_counts["四等奖"] += 1
            prize_details["四等奖"].append((lottery_numbers, user_numbers))
        else:
            prize_counts["未中奖"] += 1
            #prize_details["未中奖"].append((lottery_numbers, user_numbers))

    # 计算概率
    total = sum(prize_counts.values())
    prize_probabilities = {k: v / total for k, v in prize_counts.items()}

    return prize_counts, prize_probabilities, prize_details

def main():
    # 模拟1000次
    times = 100000
    #生成中奖号码
    lottery_numbers = generate_lottery_numbers()

    prize_counts, prize_probabilities, prize_details = simulate_lottery(times,lottery_numbers)

    # 输出结果
    print("模拟次数:", times)
    print("各奖项出现次数:", prize_counts)
    print("各奖项概率:")
    for prize, probability in prize_probabilities.items():
        print(f"{prize}: {probability:.4f} ({probability * 100:.2f}%)")

    # 打印各等奖的中奖号码
    print("\n各等奖的中奖号码详情:")
    for prize, details in prize_details.items():
        if details:  # 只打印有中奖记录的奖项
            print(f"\n{prize}（共{len(details)}次）:")
            for i, (lottery, user) in enumerate(details, 1):
                print(f"第{i}次 - 中奖号码: {lottery}, 用户号码: {user}")
    print(f"中奖号码为:{lottery_numbers}")

if __name__ == "__main__":
    main()